﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;


namespace Pitaya_IDE
{
    public partial class Form1 : Form
    {
        public static string folder_name = "文件夹";
        public static string remote_name = "Pitaya1";
        public Pitaya_Device target_device = null;

        public bool FScreenFlag = false;

        public bool panel1_vis = true, panel5_vis = true, panel14_vis = true;
        int label16_width = 2;
        int panel3_Width = 84, panel4_Height = 37, mainPanel_Height = 60;
        int panel1_Height = 51;

        public MenuPanel menuPanel;                         //菜单栏
        public Search_Device searchDevice ;                 //搜索
        private AllImage ImageCollection;                   //所有的图片 有的地方用有的地方没用到
        public MainPanel mainPanel;                         //主面板
        public FileMunePanel fileMunePanel;                 //文件目录
        public FileOperation fOLocal;
        public FileOperation fORemotely;
        public LeftSidebar leftSidebar;
        public FullScreen fullScreen;

        public Form1()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();

            mainPanel = new MainPanel(this);
            this.Controls.Add(mainPanel);
            
            searchDevice = new Search_Device(this); //搜索
            panel24.Controls.Add(searchDevice);
            searchDevice.BackColor = panel24.BackColor;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            panel5_vis = false;
            label16_width = 2;
            label17_width = 2;

            fullScreen = new FullScreen(this);
            ImageCollection = new AllImage(panel5.BackColor);
            menuPanel = new MenuPanel(panel1,this);
            fOLocal = new FileOperation(this,panel11, panel5, null, null, null, null);
            leftSidebar = new LeftSidebar(panel3,this);

            panel24.Location = panel23.Location = panel22.Location = panel21.Location = new Point(0,0);

            Refh_Search.Image = AllImage.Refh_Search[0];
            
            Rd_window();

            frameLeft.Location = new Point(0, 0);
            frameTop.Location = new Point(0, 0);

            FrameRefresh(FormWindowState.Normal == WindowState);

            frameLeft.Cursor = Cursors.SizeWE;
            frameRight.Cursor = Cursors.SizeWE;
            frameTop.Cursor = Cursors.SizeNS;
            frameBottom.Cursor = Cursors.SizeNS;


            fileMunePanel = new FileMunePanel
            {
                form1 = this,
                BackColor = panel21.BackColor
            };
            panel21.Controls.Add(fileMunePanel);
            fileMunePanel.SetupNetDisk("C:\\");

            label36.Location = new Point(20, 14);
            panel25.Location = new Point(0, 0);
            panel17.Location = new Point(0, 0);
            panel11.Size = new Size(193, 38);
            panel1.Location = new Point(0, 0);
            pictureBox14.Location = new Point(0, 0);
        }

        

        private void Form1_Resize(object sender, EventArgs e)  
        {
            Rd_window();
            if (menuPanel != null)
            { 
                menuPanel.menu_form.Hide();
                menuPanel.menu_form_vis = false;
            }
        }

        private void Form1_Deactivate(object sender, EventArgs e)
        {
            label17_puss = false;
            label20_puss = false;
            label22_puss = false;
            label23_puss = false;

            menuPanel.Set_backcolor(Color.FromArgb(1, 4, 9));

        }

        private void Form1_Activated(object sender, EventArgs e)
            =>menuPanel.Set_backcolor(Color.FromArgb(13, 17, 23));

        private void Timer1_Tick(object sender, EventArgs e)
        {
            Rd_window();
            fileMunePanel.Time_Stealth();
        } 

        Size MnSize = new Size(1264, 761);
        void Calculate_size()
        {
            LeftColumnMaxWidth = MnSize.Width - panel8_width - 300;
            if (LeftColumnWidth > LeftColumnMaxWidth) LeftColumnWidth = LeftColumnMaxWidth;
            if (LeftColumnWidth < LeftColumnMinWidth) LeftColumnWidth = LeftColumnMinWidth;

            panel14_maxheight = (panel4.Top - mainPanel.Top) / 5 * 4;
            if (panel14_height > panel14_maxheight) panel14_height = panel14_maxheight;
            if (panel14_height < panel14_minheight) panel14_height = panel14_minheight;

            panel8_maxwidth = MnSize.Width / 10 * 6;

            if (panel8_width > panel8_maxwidth) panel8_width = panel8_maxwidth;
            if (panel8_width < panel8_minwidth)  panel8_width = panel8_minwidth;

            pictureBox14_maxHeight = panel8.Height / 10 * 7;
            if (pictureBox14_Height > pictureBox14_maxHeight) pictureBox14_Height = pictureBox14_maxHeight;
            if (pictureBox14_Height < pictureBox14_minHeight) pictureBox14_Height = pictureBox14_minHeight;
            
        }

        void File_column()      // 文件栏控件位置计算
        {
            panel17.Size = new Size(panel5.Width,50);

            panel11.Location = new Point(panel17.Width - panel11.Width, 6);

            fileMunePanel.Location = new Point(0, panel17.Top + panel17.Height);
            fileMunePanel.Size = new Size(panel5.Width, panel21.Height - panel11.Height - 50);
            if (!LeftOldSize.Equals(panel21.Size))
            {
                fileMunePanel.flushed();
            }
        }
        void Find_column()      // 搜索栏控件位置计算
        { 
            
        }
        void Debug_column()     // 调试栏控件位置计算
        {
            
        }
        void Remote_column()    // 连接栏控件位置计算
        {
            panel25.Size = new Size(LeftColumnWidth, 50);

            Refh_Search.Location = new Point(panel25.Width - Refh_Search.Width - (panel25.Height - Refh_Search.Height) / 2, 
                                            (panel25.Height - Refh_Search.Height) / 2);

            searchDevice.Location = new Point(0, panel25.Top + panel25.Height);
            searchDevice.Size = new Size(LeftColumnWidth, panel24.Height - panel25.Height);
        }


        bool panel21_Visible_old = false;
        bool panel22_Visible_old = false;
        bool panel23_Visible_old = false;
        bool panel24_Visible_old = false;

        bool panel5_vis_old = false;


        Size LeftOldSize = new Size(0,0);
        void Left_sidebar()//计算左边栏控件位置
        {
            Size size = new Size(LeftColumnWidth, MnSize.Height - panel4_Height);

            panel21.Size = panel22.Size = panel23.Size = panel24.Size = size;

            if (panel21.Visible == true)
            {
                File_column();
                if (panel21_Visible_old == false)
                {
                    UpdateDrawFlag = 1;
                }
            }
            if (panel22.Visible == true)
            {
                Find_column();
            }
            if (panel23.Visible == true)
            {
                Debug_column();
            }
            if (panel24.Visible == true)
            {
                Remote_column();

                if (panel24_Visible_old == false)
                {
                    Update_device_display();
                }
            }
            panel21_Visible_old = panel21.Visible;
            panel22_Visible_old = panel22.Visible;
            panel23_Visible_old = panel23.Visible;
            panel24_Visible_old = panel24.Visible;
            LeftOldSize = size;
        }

        public void Rd_window()// 重新计算控件位置及大小
        {
            int reference1_X = 0, reference1_Y = 0;
            bool RefLctflag = false;


            if (WindowState != FormWindowState.Minimized)
                MnSize = new Size(Width, Height);

            Calculate_size();

            if (panel1.Visible != panel1_vis)
            {
                RefLctflag = true;
            }

            if (panel1_vis)
            {
                panel1.Size = new Size(MnSize.Width, panel1_Height);
                
                reference1_Y = panel1.Top + panel1.Height;

                panel1.Visible = true;

                if (menuPanel != null) menuPanel.Rd_Label();
            }
            else
            {
                panel1.Visible = false;
            }

            panel3.Location         = new Point(reference1_X, reference1_Y);
            panel3.Size             = new Size(panel3_Width, MnSize.Height - panel3.Top - panel4_Height);
            label16.Location        = new Point(reference1_X + panel3_Width - label16_width / 2, reference1_Y);
            label16.Size            = new Size(label16_width, MnSize.Height - panel3.Top - panel4_Height);
            panel4.Location         = new Point(reference1_X, MnSize.Height - panel4_Height);
            panel4.Size             = new Size(MnSize.Width, panel4_Height);

            if (leftSidebar != null) leftSidebar.Rd_Bottom();//左侧按钮位置计算

            if (panel5_vis != panel5.Visible)
            {
                RefLctflag = true;
            }

            if (panel5_vis)
            {
                panel5.Location = new Point(reference1_X + panel3_Width, reference1_Y);
                panel5.Size = new Size(LeftColumnWidth, MnSize.Height - reference1_Y - panel4_Height);

                Left_sidebar();
                
                if (!panel5_vis_old)
                {
                    Update_device_display();
                }

                label17.Location = new Point(reference1_X + panel3_Width + LeftColumnWidth - label17_width / 2, reference1_Y);
                label17.Size = new Size(label17_width, MnSize.Height - reference1_Y - panel4_Height);

                reference1_X += panel3_Width + LeftColumnWidth;
                panel5.Visible = true;
            }
            else
            {
                panel5.Visible = false;

                label17.Location    = new Point(reference1_X + panel3_Width - label17_width / 2, reference1_Y);
                label17.Size        = new Size(label17_width, MnSize.Height - reference1_Y - panel4_Height);
                reference1_X        += panel3_Width;
            }
            
            if (!label17_puss)  panel5_vis_old = panel5_vis;

            mainPanel.Location      = new Point(reference1_X, reference1_Y);
            mainPanel_Height        = panel4.Top - reference1_Y;

            if (panel14_vis) mainPanel_Height -= panel14_height;

            mainPanel.Size          = new Size(MnSize.Width - reference1_X - panel8_width, mainPanel_Height);
            panel8.Location         = new Point(MnSize.Width - panel8_width, reference1_Y);
            panel8.Size             = new Size(panel8_width, MnSize.Height - panel4.Height - reference1_Y);
            label22.Location        = new Point(label22_width, MnSize.Width - panel8_width - label22_width / 2);
            label22.Size            = new Size(panel8.Top, panel8.Height);
            pictureBox14.Size       = new Size(panel8_width, pictureBox14_Height);
            label23.Location        = new Point(label23_height, pictureBox14_Height - label23_height / 2);
            label23.Size            = new Size(0, panel8_width);

            if (panel14_vis != panel14.Visible)
            {
                RefLctflag = true;
            }
            if (panel14_vis)
            {
                panel14.Location    = new Point(reference1_X,mainPanel.Top + mainPanel.Height);
                panel14.Size        = new Size(MnSize.Width - reference1_X - panel8_width, panel14_height);
                label20.Location    = new Point(panel14.Left, panel14.Top - label20_height / 2);
                label20.Size        = new Size(panel14.Width, label20_height);
                panel14.Visible     = true;
            }
            else
            {
                panel14.Visible     = false;
                label20.Location    = new Point(panel14.Left, panel4.Top - label20_height / 2);
                label20.Size        = new Size(panel14.Width, label20_height);
            }

            label21.Location        = new Point(panel4.Left, panel4.Top - 1);
            label21.Size            = new Size(panel4.Width, 2);

            if (RefLctflag)
            {
                mainPanel.RefLct();//刷新主面板
            }
        }

        /*窗体键盘处理*/

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //this.Font = new Font(this.Font.Name, this.Font.Size);
            //label2.Text = this.Font.Size.ToString();
            //KeyCode e.KeyCode
            //KeyData e.KeyData
            //KeyValue e.KeyValue
            //label2.Text = e.KeyValue.ToString();
            if (e.KeyValue == 122)
            {
                FullScreen.F11_FullScreen();
            }
        }
        

        /*拖放处理*/

        /*左侧栏拖放处理*/

        public int LeftColumnWidth = 300;
        int LeftColumnMinWidth = 300, LeftColumnMaxWidth = 800;
        int label17_width = 2;
        bool label17_puss = false;
        private void Label17_MouseDown(object sender, MouseEventArgs e)=>label17_puss = true;
        private void Label17_MouseUp(object sender, MouseEventArgs e)
        {
            label17_puss = false;
            Rd_window();
            Left_sidebar();
            File_column();
            Find_column();
            Debug_column();
            Remote_column();
            UpdateDrawFlag = 1;
            UpdateDeviceFlag = 1;
        }
        private void Label17_MouseHover(object sender, EventArgs e)
        {
            if (panel5_vis)
                label17_width = 5;
            else
                label17_width = 3;
            label17.BackColor = Color.FromArgb(31, 111, 235);
            Rd_window();
        }
        private void Label17_MouseLeave(object sender, EventArgs e)
        {
            label17_width = 2;
            label17.BackColor = Color.FromArgb(48, 54, 61);
            Rd_window();
        }
        private void Label17_MouseMove(object sender, MouseEventArgs e)
        {
            if (label17_puss)
            {
                Point panel3Point = panel3.PointToClient(Control.MousePosition); //鼠标相对于panel3左上角的坐标
                int x = panel3Point.X - panel3.Width;
                LeftColumnWidth = x;
                if (x > LeftColumnMinWidth / 2 && !panel5_vis_old)
                {
                    panel5_vis = true;
                    label17_width = 5;
                    leftSidebar.SetSta(true);
                }
                else if(x < LeftColumnMinWidth / 2 && panel5_vis_old)
                {
                    panel5_vis = false;
                    label17_width = 3;
                    leftSidebar.SetSta(false);
                }
                panel5_vis_old = panel5_vis;
                Rd_window();

                mainPanel.RefLct();//刷新主面板
            }
        }


        /*下侧栏拖放处理*/

        int panel14_height = 170, panel14_minheight = 150, panel14_maxheight = 800;
        int label20_height = 2;
        bool label20_puss = false;
        private void Label20_MouseHover(object sender, EventArgs e)
        {
            if (panel14_vis)
                label20_height = 5;
            else
                label20_height = 3;
            label20.BackColor = Color.FromArgb(31, 111, 235);
            Rd_window();
        }
        private void Label20_MouseLeave(object sender, EventArgs e)
        {
            label20_height = 2;
            label20.BackColor = Color.FromArgb(48, 54, 61);
            Rd_window();
        }
        private void Label20_MouseDown(object sender, MouseEventArgs e)=>label20_puss = true;
        private void Label20_MouseUp(object sender, MouseEventArgs e)
        {
            label20_puss = false;
        }
        private void Label20_MouseMove(object sender, MouseEventArgs e)
        {
            if (label20_puss)
            {
                Point panel4Point = panel4.PointToClient(Control.MousePosition);
                int y = -panel4Point.Y;

                panel14_height = y;
                
                if (y > panel14_minheight / 2)
                {
                    panel14_vis = true;
                    label20_height = 5;
                }
                else
                {
                    panel14_vis = false;
                    label20_height = 3;
                }
                Rd_window();
                mainPanel.RefLct();//刷新主面板
            }
        }



        /*右侧栏拖放处理*/

        int panel8_width = 200, panel8_maxwidth = 400, panel8_minwidth = 50;
        int label22_width = 2;
        bool label22_puss = false;
        private void Label22_MouseLeave(object sender, EventArgs e)
        {
            label22_width = 2;
            label22.BackColor = Color.FromArgb(48, 54, 61);
            Rd_window();
        }
        private void Label22_MouseHover(object sender, EventArgs e)
        {
            label22_width = 6;
            label22.BackColor = Color.FromArgb(31, 111, 235);
            Rd_window();
        }
        private void Label22_MouseDown(object sender, MouseEventArgs e)=>label22_puss = true;
        private void Label22_MouseUp(object sender, MouseEventArgs e)
        {
            label22_puss = false;
        }
        private void Label22_MouseMove(object sender, MouseEventArgs e)
        {
            if (label22_puss)
            {
                Point thisPoint = this.PointToClient(Control.MousePosition);
                int x = this.Width - thisPoint.X;
                panel8_width = x;
                Rd_window();

                mainPanel.RefLct();//刷新主面板
            }
        }


        /*图像显示拖放处理*/

        int pictureBox14_Height =200, pictureBox14_maxHeight=600, pictureBox14_minHeight = 200;
        int label23_height = 2;
        bool label23_puss = false;
        private void Label23_MouseHover(object sender, EventArgs e)
        {
            label23_height = 6;
            label23.BackColor = Color.FromArgb(31, 111, 235);
            Rd_window();
        }
        private void Label23_MouseLeave(object sender, EventArgs e)
        {
            label23_height = 2;
            label23.BackColor = Color.FromArgb(48, 54, 61);
            Rd_window();
        }
        private void Label23_MouseDown(object sender, MouseEventArgs e)=>label23_puss = true;
        private void Label23_MouseUp(object sender, MouseEventArgs e)
        {
            label23_puss = false;
        }
        private void Label23_MouseMove(object sender, MouseEventArgs e)
        {
            if (label23_puss)
            {
                Point panel8Point = panel8.PointToClient(Control.MousePosition);
                int y = panel8Point.Y;
                pictureBox14_Height = y;
                Rd_window();
            }
            
        }

        /*下侧栏按钮处理*/
        private void Label19_Click(object sender, EventArgs e)
        {
            panel14_vis = !panel14_vis;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e) =>menuPanel.FormClosing(e);
        
        

        /*  文件夹栏按下 */
        private void Label_folder_MouseDown(object sender, MouseEventArgs e)
        {
            
        }
        

        private void Refh_Search_MouseEnter(object sender, EventArgs e)
        {
            Refh_Search.Image = AllImage.Refh_Search[1];
        }

        private void Refh_Search_MouseLeave(object sender, EventArgs e)
        {
            Refh_Search.Image = AllImage.Refh_Search[0];
        }

        private void Refh_Search_MouseDown(object sender, MouseEventArgs e)
        {
            searchDevice.SearchStart();
        }

        public int UpdateDeviceFlag = 0;
        public int UpdateDrawFlag = 0;

        //更新显示设备列表
        public void Update_device_display() => UpdateDeviceFlag = 1;

        private void Timer2_Tick(object sender, EventArgs e)
        {
            if (UpdateDeviceFlag == 1)
            {
                UpdateDeviceFlag = 0;
                if (searchDevice != null)
                    searchDevice.Do_Update_device_display();
            }

            if (UpdateDrawFlag == 1)
            {
                UpdateDrawFlag = 0;
                fileMunePanel.Draw();
            }
        }

        private void Image_FromFile(Control value , string file)
        {
            Label label = (Label)value;

            try
            {
                label.Image = Image.FromFile(file);
            }
            catch (FileNotFoundException)
            {
                File_err();
            }
        }

        /*文件丢失处理*/
        void File_err()
        {
            
        }



        public void FrameRefresh(bool flag)
        {
            frameRight.Location = new Point(Width - 2, 0);
            frameBottom.Location = new Point(0, Height - 2);

            frameLeft.Size = new Size(2, Height);
            frameTop.Size = new Size(Width, 2);
            frameRight.Size = new Size(2, Height);
            frameBottom.Size = new Size(Width, 2);

            frameLeft.Visible = flag;
            frameRight.Visible = flag;
            frameTop.Visible = flag;
            frameBottom.Visible = flag;
        }

        private bool FramePuss=false;
        private void Frame_MouseDown(object sender, MouseEventArgs e)
        {
            FramePuss = true;
        }

        private void Frame_MouseUp(object sender, MouseEventArgs e)
        {
            FramePuss = false;
        }

        private void Frame_MouseMove(object sender, MouseEventArgs e)
        {
            if (FramePuss)
            {
                Label label = (Label)sender;
                Point winPoint = this.PointToClient(Control.MousePosition);

                if (frameTop.Equals(label))
                {
                    if (winPoint.Y > 0)
                    {
                        if (Height <= 600)
                            return;
                    }
                    Top += winPoint.Y;
                    Height -= winPoint.Y;
                }
                else if (frameBottom.Equals(label))
                {
                    Height = winPoint.Y;
                }
                else if (frameLeft.Equals(label))
                {
                    if (winPoint.X > 0)
                    {
                        if (Width <= 800)
                            return;
                    }
                    Left += winPoint.X;
                    Width -= winPoint.X;
                }
                else if (frameRight.Equals(label))
                {
                    Width = winPoint.X;
                }

                FrameRefresh(FormWindowState.Normal == WindowState);
            }
        }

    }
}